package data

import (
	"fmt"
	"yunj/pkg/global"
	"yunj/pkg/yunj/app/model"
	"yunj/pkg/yunj/core/db"
)

// 初始化定时任务数据库
func InitCornDbData() (err error) {
	cornDb := db.Model(&model.Corn{})
	tableName := cornDb.GetTableName()
	// 创建数据表
	tableSql := "CREATE TABLE IF NOT EXISTS `" + tableName + "` (" +
		"`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '任务名称'," +
		"`status` tinyint unsigned NOT NULL DEFAULT '0' COMMENT '0 已暂停、11 运行中'," +
		"PRIMARY KEY (`name`)" +
		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='定时任务';"
	err = global.MySQL.Exec(tableSql).Error
	if err != nil {
		err = fmt.Errorf("创建数据表%s失败！%v", tableName, err)
	}
	return
}
